<?php
	// Class: Calendar
	// 
	// Calendar class to manage events in event database.
	//
	// Author: Arttu Valo
	// Date: 22.11.2006
	
	require_once("../Database/DBConnector.php");
	require_once("Event.php");
	
	class Calendar
	{
		private $db;
		private $events;
		private $calTable;
		
		// Constructor		
		public function Calendar($dbType, $serverName, $userName, $passWord, $dbName, $calTable)
		{
			$this->db = new DBConnector($dbType, $serverName, $userName, $passWord);
			$this->db->SelectDB($dbName);
			
			$this->calTable = $calTable;
			
			$events = array();
		}
		
		// Load all events		
		public function LoadEvents()
		{
			unset($events);
			$events = array();
			
			$resultset = $this->db->Query("SELECT EventID FROM ".$this->calTable); // Fill DataTable With Query
			foreach($resultset as $result)
			{
				$event = new Event($this->db, $this->calTable);
				$event->Load(intval($result->EventID));
				array_push($event);
			}
		}
		
		// Returns requested event		
		public function GetEvent($evID)
		{
			$event = new Event($this->db, $this->calTable);
			$event->Load($evID);
			return $event;
		}
		
		// Create new event		
		public function CreateEvent($evHeader, $evText, $evType, $evStart, $evEnd)
		{
			$sql = sprintf("INSERT INTO ".$this->calTable.
				" VALUES('".$evHeader."', '".$evText."', '".$evType."', '".$evStart."', '".$evEnd."')");
			$res = $this->db->Query($sql);
			return intval($res);
		}
		
		// Delete new event		
		public function DeleteEvent($evID)
		{
			return $this->db->Query("DELETE FROM ".$this->calTable." WHERE EventID=$evID");
		}
		
		// Returns events at given date (timestamp form)
		public function GetEventsForDay($date)
		{
			$events = array();
			
			$date = getdate($date);
			
			$resultset = $this->db->Query("SELECT EventID FROM ".$this->calTable." WHERE EventStart>='".
											$date["year"]."-".$date["mon"]."-".$date["mday"]." 00:00:00' AND ".
											"EventStart<='".$date["year"]."-".$date["mon"]."-".$date["mday"]." 23:59:59'");
											
			foreach($resultset as $result)
			{
				$e = new Event($this->db, $this->calTable);
				$e->Load(intval($result->EventID));
				array_push($events, $e);
			}
			return $events;
		}
		
		// Check for events occurring between to times		
		public function CountOverlappingEvents($startTime, $endTime, $eventID = -1)
		{
			$sql = "";
			//when creating new event ($eventID = -1), event id check is not needed
			if($eventID == -1)
			{
				$sql = 	"SELECT COUNT(EventID) AS NumOfEvents\n".
						"FROM  (SELECT EventID\n".
						"FROM ".$this->calTable."\n".
						"WHERE      ('".$startTime."' = EventStart OR\n".
						"('".$startTime."' < EventEnd AND '".$startTime."' > EventStart) OR\n".
						"('".$endTime."' < EventEnd AND '".$endTime."' > EventStart)))";
			}
			else
			{
				$sql = 	"SELECT COUNT(EventID) AS NumOfEvents\n".
						"FROM  (SELECT EventID\n".
						"FROM ".$this->calTable."\n".
						"WHERE      (('".$startTime."' = EventStart OR\n".
						"('".$startTime."' < EventEnd AND '".$startTime."' > EventStart) OR\n".
						"('".$endTime."' < EventEnd AND '".$endTime."' > EventStart)) AND EventID != $eventID))";
			}
				
			// DERIVEDTBL needs to be appended to the query for sql server to accept it
			if($this->db->GetType() == DBConnector::DBC_MSSQL)
			{
				$sql .= " DERIVEDTBL";
			}
			$resultset = $this->db->Query($sql);
			return intval($resultset[0]->NumOfEvents);
		}
	}
?>